<?php
	require '../../global.php';
	$id = $_POST['id'];
	if(empty($id)){
		header("Location: /tool/index.php");
		exit;
	}
	$name = $_POST['name'];
	if(empty($name)){
		header("Location: /tool/index.php");
		exit;
	}
	$html = $_POST['html'];
	if(empty($html)){
		header("Location: /tool/index.php");
		exit;
	}
	$author = $_POST['author'];
	$site = $_POST['site'];
	$version = $_POST['version'];
	$desc = $_POST['desc'];
	$body = '';

	
	//判断ID是否已经存在
	if(file_exists(ROOT.'/external/widgets/'.$id)){
		header("Location: /tool/index.php?id={$id}");
		exit;
	}else{
		//创建目录
		mkdir(ROOT.'/external/widgets/'.$id,0777);
	}

	//创建widget.info.php
	create_file(ROOT.'/external/widgets/'.$id.'/widget.info.php',"<?php
	return array(
	    'name'      => '{$id}',
	    'display_name'  => '{$name}',
	    'author'    => '{$author}',
	    'website'   => '{$site}',
	    'version'   => '{$version}',
	    'desc'      => '{$desc}',
	    'configurable'  => true,
	);
?>");
	//创建main.widget.php
	create_file(ROOT.'/external/widgets/'.$id.'/main.widget.php',"<?php
	class ".ucfirst($id)."Widget extends BaseWidget{
		var \$_name = '{$id}';
		function _get_data(){
			
		}
	}
?>");
	
	if(file_exists($file)){
		header("Location: /tool/index.php?id={$id}");
		exit;
	}
	
	//创建模板文件
	create_file(ROOT.'/external/widgets/'.$id.'/'.$html.'widget.html','<!-- 模板可以留空，默认数据变量：{$widget_data} -->');
	
	header("Location: /tool/index.php?id={$id}");